local GridLogic = require("bag.GridLogic")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")

--返回寻宝仓库列表
function sendCowryBagList(human, no_always_send)
  if human.db.cowryBagLen == 0 then 
    human.db.cowryBagLen = ItemDefine.COWRY_MAX_CAP
  end 
  Grid.makeList(human,human.db.cowryBag, ItemDefine.COWRY_MAX_CAP, Msg.gc.GC_COWRY_BAG_LIST, not no_always_send and Msg.gc.GC_COWRY_BAG_CLEAR)
  return true 
end

--发送寻宝仓库某格子物品的信息
function sendCowryBagItemChange(human, index)  
  local msgRet = Msg.gc.GC_COWRY_BAG_ITEM_CHANGE
  GridLogic.makeGrid(human.db.cowryBag[index] ,msgRet.itemData, index,human)
  Msg.send(msgRet, human.fd)  
  return true   
end


--寻宝仓库整理
function sortCowryBag(human)
  Grid.sort(human.db.cowryBag, ItemDefine.COWRY_MAX_CAP)
  return sendCowryBagList(human)
end

function clearCowryBag(human)
  for i=0, ItemDefine.COWRY_MAX_CAP - 1 do
    human.db.cowryBag[i].id = 0
  end
  sendCowryBagList(human)
end


--检查下标合法
function checkCowryBagIndex(human, index)
  return GridLogic.checkIndex(human.db.cowryBag, index, ItemDefine.COWRY_MAX_CAP)
end

local function swap(human, srcIndex, tarIndex)
  if not checkCowryBagIndex(human,srcIndex) or not checkCowryBagIndex(human,tarIndex) then
    return false
  end
  local srcGrid = human.db.cowryBag[srcIndex]
  if srcGrid.id == 0 then
      return
  end

  local tarGrid = human.db.cowryBag[tarIndex]

  if srcGrid.id == tarGrid.id and ItemDefine.isItem(srcGrid.id) and ItemDefine.canAppend(srcGrid.id) and tarGrid.bind == 1 and srcGrid.bind == 0 then
    local cnt = math.min(srcGrid.cnt, ItemDefine.CAP_PER_GRID - tarGrid.cnt)
    tarGrid.cnt = tarGrid.cnt + cnt
    srcGrid.cnt = srcGrid.cnt - cnt
    if srcGrid.cnt <= 0 then
      human.db.cowryBag[srcIndex].id = 0
    end
  else
    local ret = GridLogic.swap(human.db.cowryBag, ItemDefine.COWRY_MAX_CAP,srcIndex,tarIndex)
    if not ret then
      assert()
      return
    end
  end
  
  if srcGrid.cnt == 0 then
    srcGrid.id = 0
  end
    
  return true
end

--寻宝仓库物品移动
function swapCowryBag(human, srcIndex, tarIndex)
  local ret = swap(human, srcIndex, tarIndex)
  if ret then
    sendCowryBagItemChange(human, srcIndex)
    sendCowryBagItemChange(human, tarIndex)
  end 
  return ret
end

--背包寻宝仓库间，物品移动
function swapCowryBag(human, posBag, posCowry)
  if not posBag or not posCowry then
    return false
  end
  return GridLogic.swap2(human.db.cowryBag, posCowry, human.db.bag, posBag)
end

pos = pos or {}
function addItem(human, id, bind, cnt, qualityOut, lifeTime)
    lifeTime = lifeTime or 0
    if not bind or bind == 0 then
        bind = 0
    else
        bind = 1
    end

    local logGrid
    local cap = ItemDefine.COWRY_MAX_CAP
    local cowryBag = human.db.cowryBag
    local capPerGrid = ItemDefine.CAP_PER_GRID
    local posLen = 0
    local grid
    for i = 0,cap-1 do
        grid = cowryBag[i]
        if grid.id == id and grid.bind == bind and grid.cnt < capPerGrid and ItemDefine.canAppend(grid.id) and grid.lifeTime == lifeTime then
            posLen = posLen + 1
            pos[posLen] = i
            local cntReal = math.min(capPerGrid - grid.cnt, cnt)
            grid.cnt = grid.cnt + cntReal
            logGrid = grid
            cnt = cnt - cntReal
            if cnt <= 0 then
                break
            end
        end
    end

    if cnt > 0 then
        for i = 0,cap-1 do
            grid = cowryBag[i]
            if grid.id == 0 then
                posLen = posLen + 1
                pos[posLen] = i
                local cntReal = math.min(capPerGrid, cnt)
                if not ItemDefine.canAppend(id) then
                    cntReal = 1
                end
                cowryBag[i] = Grid.create(grid,id,cntReal,bind,qualityOut,lifeTime)
                logGrid = cowryBag[i]
                if strengthLv then
                  logGrid.data.equip.strengthLv = strengthLv
                end
                cnt = cnt - cntReal
                if cnt <= 0 then
                    break
                end
            end
        end
    end

    for i = 1 ,posLen do
        if human.db._id then
            sendCowryBagItemChange(human, pos[i], op)
        end
    end

    return true,logGrid
end

--提取单个宝物
function extractOne(human, index)
  local grid = human.db.cowryBag[index]
  if grid.id == 0 then
    return
  end
  if not BagLogic.checkCanAddItem(human, grid.id, grid.bind, grid.cnt) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_GRID_NOT_ENOUGH)
  end
  BagLogic.addItem(human, grid.id, grid.bind, grid.cnt, "fulidating", nil, grid.lifeTime)
  grid.id = 0
  sendCowryBagItemChange(human, index)
end

function extractALL(human)
  local item = {}
  local index = 0
  for i=0, ItemDefine.COWRY_MAX_CAP - 1 do
    local grid = human.db.cowryBag[i]
    if grid.id ~= 0 then
      index = index + 1
      item[index] = {}
      item[index][1] = grid.id
      item[index][2] = grid.cnt
      item[index][3] = grid.bind
      item[index][4] = grid.lifeTime 
    end
  end
  if not BagLogic.checkCanAddItemList(human, item, #item) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_GRID_NOT_ENOUGH)
  end
  for i=1, #item do
    BagLogic.addItem(human, item[i][1], item[i][3], item[i][2], "fulidating", nil, item[i][4])
  end
  clearCowryBag(human)
end

--判断是否还可以移入
function checkCanAddItemList(human, item)
  local cowryBag = human.db.cowryBag
  local cap = ItemDefine.COWRY_MAX_CAP
  local capPerGrid = ItemDefine.CAP_PER_GRID
  
  for i = 0, cap-1 do
    local grid = cowryBag[i]
    for kk=1, #item do
      if not item[kk][5] then
        local cnt = item[kk][2]
        local bind = item[kk][3]
        if bind == 0 or not bind then
          bind = 0
        else
            bind = 1
        end
        if cnt < 1 then
          return false
        end
        if not ItemDefine.isValidId(item[kk][1]) then
          return false
        end
  
        if grid.id == 0 or grid.id == item[kk][1] and grid.bind == bind and grid.lifeTime == item[kk][4] and grid.cnt < capPerGrid and ItemDefine.canAppend(grid.id) then
          if not ItemDefine.canAppend(item[kk][1]) then
              cnt = cnt - 1
          elseif grid.id ~= 0 then
              cnt = cnt - (capPerGrid - grid.cnt)
          else
              cnt = cnt - capPerGrid
          end
          if cnt <= 0 then
            item[kk][5] = true          
            break
          end
        end
      end
    end    
  end  
  for i=1, #item do
    if not item[i][5] then
      return false
    end
  end
  return true
end


